package com.sanley.coronavirus.controller;

import com.github.pagehelper.PageInfo;
import com.sanley.coronavirus.bo.ReportBO;
import com.sanley.coronavirus.common.api.CommonResult;
import com.sanley.coronavirus.entity.Report;
import com.sanley.coronavirus.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @Author: ming
 * @create: 2022-11-17 18:49
 * @program: coronavirus
 */
@Controller
@RequestMapping("/report")
@Api(value = "ReportController", description = "报备管理")
public class ReportController {

    @Autowired
    private ReportService reportService;


    @ApiOperation("添加报备")
    @ResponseBody
    @PostMapping("/user/add")
    public CommonResult add(@RequestBody Report report, Principal principal) {
        int flag = reportService.add(report, principal.getName());
        if (flag == 1) {
            return CommonResult.success(null, "报备成功");
        } else {
            return CommonResult.failed("报备失败");
        }
    }

    @ApiOperation("用户获取报备记录")
    @ResponseBody
    @GetMapping("/user/list")
    public CommonResult list(Principal principal) {
        List<Report> reports = reportService.getReportByUsername(principal.getName());
        return CommonResult.success(reports);
    }

    @ApiOperation("获取报备总记录")
    @ResponseBody
    @GetMapping("/list")
    public CommonResult listAll(@RequestParam(value = "size", defaultValue = "5") Integer size,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "keyword", required = false) String keyword) {
        if (keyword != null) {
            keyword = keyword.trim();
            if (keyword.length() == 0) {
                keyword = null;
            }
        }
        List<ReportBO> reportBOS = reportService.listAll(page, size, keyword);
        PageInfo<ReportBO> pageInfo = new PageInfo<>(reportBOS);
        return CommonResult.success(pageInfo);
    }


}
